package com.zhucan.tenant.mongo;


import com.zhucan.tenant.mongo.extension.MongoTenantDatabaseNameGenerator;
import com.zhucan.tenant.mongo.extension.TenantProvider;

/**
 * @author My Lord zhuCan
 * @description 芒果的上下文环境信息
 * @since 2022-12-09 11:10
 **/
public interface MongoContextHolder {

    /**
     * 租户数据库名
     *
     * @return database
     */
    String tenantDatabaseName();

    class DefaultMongoContextHolder implements MongoContextHolder {

        private final MongoTenantDatabaseNameGenerator nameGenerator;

        private final TenantProvider tenantProvider;

        public DefaultMongoContextHolder(MongoTenantDatabaseNameGenerator nameGenerator, TenantProvider tenantProvider) {
            this.nameGenerator = nameGenerator;
            this.tenantProvider = tenantProvider;
        }

        @Override
        public String tenantDatabaseName() {
            return nameGenerator.generateDatabaseName(tenantProvider.tenantCode());
        }
    }
}
